package com.car.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    /**
     * 接收商品订单延迟检查队列的队列
     *
     * @return
     */
    @Bean
    public Queue goodsOrderCheckQueue() {
        return new Queue("goods_order_check_queue");
    }


    /**
     * 商品订单延迟检查队列
     *
     * @return
     */
    @Bean
    public Queue goodsOrderCheckDelayQueue() {
        Map<String, Object> params = new HashMap<>();
        //定义该队列中消息的生存时间 单位毫秒 生存时间最终对应业务的延迟执行时间
        params.put("x-message-ttl", 60 * 1000 * 15);
        // x-dead-letter-exchange 属性用于声明死信转发到的交换机
        params.put("x-dead-letter-exchange", "goods_order_check_exchange");
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的路由键名称。
        params.put("x-dead-letter-routing-key", "goods_order");
        return new Queue("goods_order_check_delay_queue", true, false, false, params);
    }


    /**
     * goodsOrder交换机
     *
     * @return
     */
    @Bean
    public DirectExchange goodsOrderCheckExchange() {
        return new DirectExchange("goods_order_check_exchange");
    }


    /**
     * goodsOrder路由
     *
     * @return
     */
    @Bean
    public Binding goodsOrderCheckBinding() {
        return BindingBuilder.bind(goodsOrderCheckQueue()).to(goodsOrderCheckExchange()).with("goods_order");
    }


    /**
     * 接收服务订单延迟检查队列的队列
     *
     * @return
     */
    @Bean
    public Queue serveOrderCheckQueue() {
        return new Queue("serve_order_check_queue");
    }


    /**
     * 服务订单延迟检查队列
     *
     * @return
     */
    @Bean
    public Queue serveOrderCheckDelayQueue() {
        Map<String, Object> params = new HashMap<>();
        //定义该队列中消息的生存时间 单位毫秒 生存时间最终对应业务的延迟执行时间
        params.put("x-message-ttl", 60 * 1000 * 15);
        // x-dead-letter-exchange 属性用于声明死信转发到的交换机
        params.put("x-dead-letter-exchange", "serve_order_check_exchange");
        // x-dead-letter-routing-key 声明了这些死信在转发时携带的路由键名称。
        params.put("x-dead-letter-routing-key", "serve_order");
        return new Queue("serve_order_check_delay_queue", true, false, false, params);
    }


    /**
     * serveOrder交换机
     *
     * @return
     */
    @Bean
    public DirectExchange serveOrderCheckExchange() {
        return new DirectExchange("serve_order_check_exchange");
    }


    /**
     * serveOrder路由
     *
     * @return
     */
    @Bean
    public Binding serveOrderCheckBinding() {
        return BindingBuilder.bind(serveOrderCheckQueue()).to(serveOrderCheckExchange()).with("serve_order");
    }
}
